package com.team.six.bluecatshop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.six.bluecatshop.domain.Product;
import com.team.six.bluecatshop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangpeng
 * @date 2019-08-07  10:44
 */
@Controller
@RequestMapping("/search")
public class ProductSearchController {

    @Autowired
    ProductService productService;

    private int count = 0;

    ///**
    // * 价格区间查询
    // */
    //@RequestMapping("/priceBetween")
    //public String priceBetween(String keyword, String priceFont, String priceFoot, Model model, @RequestParam(value = "pageNum",defaultValue = "1")String pageNum){
    //    PageHelper.startPage(Integer.parseInt(pageNum),4);
    //    List<Product> productList = productService.findByPriceSection(keyword,priceFont,priceFoot);
    //
    //    //得到分页的结果对象
    //    PageInfo<Product> productPageInfo = new PageInfo<>(productList);
    //    //得到分页中的products条目对象
    //    List<Product> products = productPageInfo.getList();
    //    model.addAttribute("productList",products);
    //    model.addAttribute("keyword",keyword);
    //    model.addAttribute("priceFont",priceFont);
    //    model.addAttribute("priceFoot",priceFoot);
    //    List<String> str = new ArrayList<>();
    //    for (int i=1;i<6;i++){
    //        str.add(String.valueOf(i));
    //    }
    //    model.addAttribute("str",str);
    //    return "search";
    //}
    //
    ///**
    // * 价格排序，价格区间查询
    // */
    //@RequestMapping("/findByPriceAsc")
    //public String findByPriceAsc(String keyword, String priceFont, String priceFoot,Model model,String sort,String Youfei,String kucun){
    //    count = count + 1;
    //    List<Product> productList = productService.findByPriceAsc(keyword,priceFont,priceFoot,count);
    //    model.addAttribute("productList",productList);
    //    model.addAttribute("keyword",keyword);
    //    model.addAttribute("priceFont",priceFont);
    //    model.addAttribute("priceFoot",priceFoot);
    //    return "search";
    //}
    //
    //
    ///**
    // * 商品评分排序，价格区间
    // */
    //@RequestMapping("/findByComment")
    //public String findByComment(String keyword, String priceFont, String priceFoot,Model model,String sort,String Youfei,String kucun){
    //    count = count + 1;
    //    PageHelper.startPage(1,8);
    //    List<Product> productList = productService.findByComment(keyword,priceFont,priceFoot,count);
    //    //得到分页的结果对象
    //    PageInfo<Product> productPageInfo = new PageInfo<>(productList);
    //    //得到分页中的products条目对象
    //    List<Product> products = productPageInfo.getList();
    //    model.addAttribute("productList",products);
    //    model.addAttribute("keyword",keyword);
    //    model.addAttribute("priceFont",priceFont);
    //    model.addAttribute("priceFoot",priceFoot);
    //    return "search";
    //}
    //
    ///**
    // * 是否包邮，价格排序，价格区间查询
    // */
    //public List<Product> findByYoufei(String keyword, String priceFont, String priceFoot,Model model){
    //    //count = count + 1;
    //    return productService.findByYoufei(keyword,priceFont,priceFoot,count);
    //}


    /**
     * 搜索 ：
     */
    @RequestMapping("/commonSearch")
    public String commonSearch(String keyword, String priceFont, String priceFoot,@RequestParam(value = "pageNum",defaultValue = "1")String pageNum
            ,Model model,String sort,String Youfei,String kucun,@RequestParam(value = "YoufeiBiaoji",defaultValue = "")String YoufeiBiaoji,@RequestParam(value = "kucunBiaoji",defaultValue = "")String kucunBiaoji){
        count += 1;
        PageHelper.startPage(Integer.parseInt(pageNum),8);
        List<Product> productList = productService.findSearchByExample(keyword, priceFont, priceFoot, sort, YoufeiBiaoji, kucunBiaoji,count);

        PageInfo<Product> productPageInfo = new PageInfo<>(productList);
        //得到分页中的products条目对象
        List<Product> products = productPageInfo.getList();
        model.addAttribute("productList",products);
        model.addAttribute("keyword",keyword);
        model.addAttribute("priceFont",priceFont);
        model.addAttribute("priceFoot",priceFoot);
        model.addAttribute("sort",sort);
        model.addAttribute("Youfei",YoufeiBiaoji);
        model.addAttribute("kucun",kucunBiaoji);
        List<String> str = new ArrayList<>();
        for (int i=1;i<6;i++){
            str.add(String.valueOf(i));
        }
        model.addAttribute("str",str);
        return "search";
    }

}
